<template>
  <u-empty mode="data" v-if="result.data.length <= 0 && result.isEnd"></u-empty>
  <view class="car-maintenance-store" v-else>
    <view class="list">
      <view class="flex flex-jc-c u-m-20" v-if="result.pageIndex === 1 && !result.isEnd"><u-loading mode="circle"></u-loading></view>
      <block v-for="(item, index) in result.data" :key="index">
        <car-maintenance-item :item="item" @click="onClickItem" />
      </block>
      <view class="loading-status" v-if="result.pageIndex !== 1"><u-loadmore :status="result.status" /></view>
    </view>
  </view>
</template>

<script>
import carMaintenanceItem from './components/carMaintenanceItem.vue'
export default {
  components: {
    carMaintenanceItem
  },
  data() {
    return {
      result: {
        pageIndex: 1,
        pageSize: 20,
        isEnd: false,
        loading: false,
        dlon: 0,
        dlat: 0,
        status: 'loading',
        data: []
      },
      type: '' // 类型
    }
  },
  onLoad(option) {
    this.type = option.type || ''
    this.$commen.getLocation(data => {
      this.result.dlon = data.lon
      this.result.dlat = data.lat
      this.getList()
    })
  },
  methods: {
    // 计算总共有多少页
    pageCount (totalnum,limit){
      return totalnum > 0 ? ((totalnum < limit) ? 1 : ((totalnum % limit) ? (parseInt(totalnum / limit) + 1) : (totalnum / limit))) : 0
    },
    async getList() {
      let res = this.result
      if (res.loading || res.isEnd) {
        return false;
      }
      res.loading = true;
      this.result = res
      
      try{
        let resData = await this.$api.store.GetMaintainStoreList({
          districtid: 0,
          StoreType: '',
          dlon: res.dlon,
          dlat: res.dlat,
          pageIndex: res.pageIndex,
          pageSize: res.pageSize,
        })
        let list = resData.data.list
        // 如果返回数据列表为空，或者当前页数大于总页数代表已经到最后一页了
        if(list.length <= 0 || res.pageIndex >= this.pageCount(resData.data.total, res.pageSize)) {
          res.isEnd = true
          res.status = 'nomore'
        }
        
        if (list.length > 0) {
          res.data = res.data.concat(list)
          res.pageIndex = res.pageIndex + 1
        }
        res.loading = false
        this.result = res
      }catch(e){
        //TODO handle the exception
      }
    },
    // 点击
    onClickItem(data) {
      if(this.type === 'selectStore') {
        uni.$emit('onSelectStore', data)
        uni.navigateBack({
          delta: 1
        })
      }
    }
  },
  onReachBottom() {
    this.getList()
  }
}
</script>

<style lang="scss">
page{
  background-color: #f6f6f6;
}
.car-maintenance-store{
  .list{
    overflow: hidden;
  }
}
</style>
